-- CIS 552, Mini-project
-- by Mike Gottlieb and Reilly Grant

-- |Current state for a player.
module Player (T, -- type
               player, -- constructor
               name, hand, money, inPlay, -- accessors
               bets, dealTo, draws, discards, folds, reset -- functions
              ) where

import Data.List
import qualified Card
import qualified Deck

data T = Player { name::String,
                  hand::[Card.T],
                  money::Int,
                  inPlay::Bool }

-- |Pretty printing of a Player
instance Show T where
    show p = "Player " ++ (name p) ++ "\n" ++
             "Hand: " ++ (unwords $ map show $ hand p) ++ "\n" ++
             "Money: " ++ (show $ money p) ++ "\n" ++
             if (not $ inPlay p) then "Not in play.\n" else ""

defaultMoney :: Int
defaultMoney = 1024

-- |Construct a new player.
player :: String -> T
player n = Player { name = n, hand = [], money = defaultMoney, inPlay = True }

-- |Subtract the amount bet from the player's money.
bets :: T -> Int -> T
bets p a = p { money = money p - a }

-- |Replace the player's hand with the dealt one.
dealTo :: T -> [Card.T] -> T
dealTo p h = p { hand = h }

-- |Draw n cards from the deck into the player's hand.
draws :: T -> Deck.T -> Int -> (T, Deck.T)
draws p d n = (p', d')
    where (cs, d') = Deck.deal n d
          p' = dealTo p (hand p ++ cs)

-- |Discard cards from the player's hand.
discards :: T -> [Card.T] -> T
discards p cs = p { hand = hand p \\ cs }

-- |The player folds.
folds :: T -> T
folds p = p { inPlay = False }

-- |Reset the player to a pre-play state. (With money preserved.)
reset :: T -> T
reset p = p { hand = [], inPlay = True }
